﻿using System;

namespace BankSimulation
{
    // 基类：Account
    public class Account
    {
        // 字段
        private string _accountNumber;
        private decimal _balance;

        // 属性
        public string AccountNumber
        {
            get { return _accountNumber; }
            private set { _accountNumber = value; }
        }

        public decimal Balance
        {
            get { return _balance; }
            protected set { _balance = value; }
        }

        // 构造函数
        public Account(string accountNumber, decimal initialBalance)
        {
            _accountNumber = accountNumber;
            _balance = initialBalance;
        }

        // 方法：存款
        public virtual void Deposit(decimal amount)
        {
            if (amount > 0)
            {
                Balance += amount;
                Console.WriteLine($"存款成功，当前余额: {Balance}");
            }
            else
            {
                Console.WriteLine("存款金额必须大于0");
            }
        }

        // 方法：取款
        public virtual void Withdraw(decimal amount)
        {
            if (amount > 0 && amount <= Balance)
            {
                Balance -= amount;
                Console.WriteLine($"取款成功，当前余额: {Balance}");
            }
            else
            {
                Console.WriteLine("取款金额无效或余额不足");
            }
        }

        // 索引器
        public decimal this[string action, decimal amount]
        {
            get
            {
                if (action == "balance")
                {
                    return Balance;
                }
                throw new ArgumentException("无效的操作");
            }
            set
            {
                if (action == "deposit")
                {
                    Deposit(value);
                }
                else if (action == "withdraw")
                {
                    Withdraw(value);
                }
                else
                {
                    throw new ArgumentException("无效的操作");
                }
            }
        }
    }

    // 子类：CreditAccount
    public class CreditAccount : Account
    {
        // 新增字段：信用额度
        private decimal _creditLimit;

        // 新增属性：信用额度
        public decimal CreditLimit
        {
            get { return _creditLimit; }
            private set { _creditLimit = value; }
        }

        // 构造函数
        public CreditAccount(string accountNumber, decimal initialBalance, decimal creditLimit)
            : base(accountNumber, initialBalance)
        {
            _creditLimit = creditLimit;
        }

        // 重写取款方法
        public override void Withdraw(decimal amount)
        {
            if (amount > 0 && amount <= Balance + CreditLimit)
            {
                Balance -= amount;
                Console.WriteLine($"取款成功，当前余额: {Balance}");
            }
            else
            {
                Console.WriteLine("取款金额无效或超出信用额度");
            }
        }

        // 新增方法：调整信用额度
        public void AdjustCreditLimit(decimal newLimit)
        {
            if (newLimit >= 0)
            {
                CreditLimit = newLimit;
                Console.WriteLine($"信用额度已调整为: {CreditLimit}");
            }
            else
            {
                Console.WriteLine("信用额度不能为负数");
            }
        }
    }

    // ATM类
    public class ATM
    {
        // 方法：存款
        public void Deposit(Account account, decimal amount)
        {
            account.Deposit(amount);
        }

        // 方法：取款
        public void Withdraw(Account account, decimal amount)
        {
            account.Withdraw(amount);
        }

        // 方法：查询余额
        public void CheckBalance(Account account)
        {
            Console.WriteLine($"当前余额: {account["balance", 0]}");
        }
    }

    // 主程序入口
    class Program
    {
        static void Main(string[] args)
        {
            // 创建普通账户
            Account myAccount = new Account("123456789", 1000);
            ATM atm = new ATM();

            // 存款
            atm.Deposit(myAccount, 500);
            // 取款
            atm.Withdraw(myAccount, 200);
            // 查询余额
            atm.CheckBalance(myAccount);

            // 创建信用账户
            CreditAccount myCreditAccount = new CreditAccount("987654321", 1000, 500);
            // 使用信用额度取款
            atm.Withdraw(myCreditAccount, 1200);
            // 调整信用额度
            myCreditAccount.AdjustCreditLimit(1000);
            // 再次取款
            atm.Withdraw(myCreditAccount, 1200);
            // 查询余额
            atm.CheckBalance(myCreditAccount);
        }
    }
}